package com.weipai.springboot.module.service.impl;
import com.weipai.springboot.enums.RetCode;
import com.weipai.springboot.model.GoodsCollection;
import com.weipai.springboot.model.ShopCollection;
import com.weipai.springboot.model.api.Response;
import com.weipai.springboot.module.dao.GoodsCollectionDao;
import com.weipai.springboot.module.dao.ShopCollectionDao;
import com.weipai.springboot.module.mapper.GoodsCollectionMapper;
import com.weipai.springboot.module.service.CollectionService;
import com.weipai.springboot.module.util.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

/**
 * Created by ${lyh} on 2018/8/27.
 *    收藏模块
 *
 */
@Service
public class CollectionServiceImpl implements CollectionService {


    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    GoodsCollectionDao goodsCollectionDao;
    @Autowired
    ShopCollectionDao shopCollectionDao;
    @Autowired
    GoodsCollectionMapper goodsCollectionMapper;


    //收藏商品
    @Override
    public Response saveGoodsCollection(String customerId, String goodsId) {
        Response response = new Response();
        GoodsCollection goodsCollection = new GoodsCollection();
        //验证是否存在此商品
            List<GoodsCollection> goodsCollections = goodsCollectionDao.verGoodsCollection(customerId,goodsId);
            if(goodsCollections.size()>0){
                response.setCode(RetCode.REPEAT_DATA);
            }else{
                goodsCollection.setCollectionId( UUID.randomUUID().toString().replaceAll("-", ""));
                goodsCollection.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
                goodsCollection.setCustomerId(customerId);
                goodsCollection.setGoodsId(goodsId);
                if(1==goodsCollectionDao.insert(goodsCollection)){
                    response.setCode(RetCode.SUCCESS);
                }else {
                    response.setCode(RetCode.REQUEST_ERROR);
                }
            }
        return response;
    }

    // 商品详情 判断是否 收藏此商品
    @Override
    public Response varGoodsCollection(String customerId, String goodsId) {
        Response response = new Response();
        List<GoodsCollection> goodsCollections = goodsCollectionDao.verGoodsCollection(customerId,goodsId);
        if(goodsCollections.size()>0){
            response.put("goodsCollection",1);
        }else {
            response.put("goodsCollection",0);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }


    //收藏商店
    @Override
    public Response saveShopCollection(String customerId, String shopId) {
        Response response = new Response();
        ShopCollection shopCollection = new ShopCollection();
        //验证是否存在此商品
        List<ShopCollection> shopCollections = shopCollectionDao.verGoodsCollection(customerId,shopId);
        if(shopCollections.size()>0){
            response.setCode(RetCode.REPEAT_DATA);
        }else {
            shopCollection.setCollectionId(UUID.randomUUID().toString().replaceAll("-", ""));
            shopCollection.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
            shopCollection.setCustomerId(customerId);
            shopCollection.setShopId(shopId);
            if (1 == shopCollectionDao.insert(shopCollection)) {
                response.setCode(RetCode.SUCCESS);
            } else {
                response.setCode(RetCode.REQUEST_ERROR);
            }
        }
        return response;
    }

    @Override
    public List<GoodsCollection> selectAllByGoodsCollection(String customerId, Integer page, Integer size) {
        return goodsCollectionDao.selectAllByGoodsCollection(customerId,page,size);
    }

    @Override
    public List<ShopCollection> selectAllByShopCollection(String customerId, Integer page, Integer size) {
        return shopCollectionDao.selectAllByShopCollection(customerId,page,size);
    }

    //根据id 删除单个商品
    @Override
    public Response deleteById(String customerId, String goodsId) {
        Response response = new Response();
        if(1==goodsCollectionDao.deleteByPrimaryKey(customerId,goodsId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }

    //批量 删除多个
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response deleteByIdMore(String customerId, String[] goodsIds) {
        Response response = new Response();
        int rr = 0;
        if(goodsIds.length>0) {
            for (String goodsId : goodsIds) {
               goodsCollectionDao.deleteByPrimaryKey(customerId,goodsId);
               rr=1;
            }
        }else{
            response.setCode(RetCode.NO_DATA);
        }
        if(rr>0){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }



    //根据id 取消商铺
    @Override
    public Response deleteShopById(String customerId,String shopId) {
        Response response = new Response();
        if(1==shopCollectionDao.deleteByPrimaryKey(customerId,shopId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }




    //批量添加商品收藏
    @Override
    @Transactional(readOnly=false, propagation = Propagation.REQUIRED)
    public Response allAdd(String customerId, String[] goodsId) {
        Response response = new Response();
        GoodsCollection goodsCollection = new GoodsCollection();
        int rr = 0;
        if(goodsId.length>0) {
            for (String id : goodsId) {

                List<GoodsCollection> goodsCollections = goodsCollectionDao.verGoodsCollection(customerId,id);
                if(goodsCollections.size()==0){
                    goodsCollection.setCollectionId( UUID.randomUUID().toString().replaceAll("-", ""));
                    goodsCollection.setCreateTime(DateUtils.timeFormat(System.currentTimeMillis()));
                    goodsCollection.setCustomerId(customerId);
                    goodsCollection.setGoodsId(id);
                    rr = goodsCollectionDao.insert(goodsCollection);
                }
            }
        }else{
            goodsCollection.setCustomerId(customerId);
            goodsCollection.setGoodsId(goodsId.toString());
            rr = goodsCollectionDao.insert(goodsCollection);
        }
        response.setCode(RetCode.SUCCESS);
        return response;
    }




    //一键清空 商品收藏
    @Override
    public Response deleteByAll(String customerId) {
        Response response = new Response();
        if(1==goodsCollectionDao.deleteByAll(customerId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.REQUEST_ERROR);
        }
        return response;
    }


    //一键清空 商店收藏
    @Override
    public Response deleteByAllShop(String customerId) {
        Response response = new Response();
        if(1==shopCollectionDao.deleteByAllShop(customerId)){
            response.setCode(RetCode.SUCCESS);
        }else {
            response.setCode(RetCode.DEL_ERROR);
        }
        return response;
    }

    @Override
    public Integer countByAll(String customerId) {
        return goodsCollectionMapper.countByAll(customerId);
    }



}


